<template>
    <div class="common-layout">
        <el-container>
            <el-header>
                <div class="header-content">
                    <div class="logo">
                        <div class="logo-icon-wrapper">
                            <el-icon class="logo-icon">
                                <Setting />
                            </el-icon>
                        </div>
                        <div class="logo-text-wrapper">
                            <span class="logo-text">爱睡觉溯源管理平台</span>
                            <span class="logo-subtitle">Admin System</span>
                        </div>
                    </div>
                    <div class="header-right">
                        <div class="header-actions">
                            <el-button class="notification-btn" :icon="Bell" circle />
                            <el-dropdown>
                                <div class="user-info">
                                    <el-avatar :size="40"
                                        src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
                                    <div class="user-details">
                                        <span class="user-name">{{ userName }}</span>
                                        <span class="user-role">{{ roleName }}</span>
                                    </div>
                                    <el-icon>
                                        <ArrowDown />
                                    </el-icon>
                                </div>
                                <template #dropdown>
                                    <el-dropdown-menu>
                                        <el-dropdown-item @click="goUserProfile">
                                            <el-icon>
                                                <User />
                                            </el-icon>
                                            个人设置
                                        </el-dropdown-item>
                                        <el-dropdown-item @click="goSystemSetting">
                                            <el-icon>
                                                <Setting />
                                            </el-icon>
                                            系统设置
                                        </el-dropdown-item>
                                        <el-dropdown-item divided @click="logout">
                                            <el-icon>
                                                <SwitchButton />
                                            </el-icon>
                                            退出登录
                                        </el-dropdown-item>
                                    </el-dropdown-menu>
                                </template>
                            </el-dropdown>
                        </div>
                    </div>
                </div>
            </el-header>
            <el-container>
                <el-aside width="280px">
                    <div class="sidebar-header">
                        <h3 class="sidebar-title">导航菜单</h3>
                    </div>
                    <el-menu active-text-color="#fff" background-color="transparent" class="el-menu-vertical-demo"
                        default-active="1" text-color="#64748b" @open="handleOpen" @close="handleClose" router>

                        <el-menu-item index="/user-profile" class="menu-item">
                            <el-icon>
                                <House />
                            </el-icon>
                            <span>首页</span>
                        </el-menu-item>

                        <el-sub-menu index="1" class="menu-submenu" v-if="userName == 'admin'">
                            <template #title>
                                <el-icon>
                                    <User />
                                </el-icon>
                                <span>RBAC管理</span>
                            </template>
                            <el-menu-item index="/PermissionGet" class="submenu-item">
                                <el-icon>
                                    <Key />
                                </el-icon>
                                <span>权限管理</span>
                            </el-menu-item>
                            <el-menu-item index="/RoleGet" class="submenu-item">
                                <el-icon>
                                    <Avatar />
                                </el-icon>
                                <span>角色管理</span>
                            </el-menu-item>
                            <el-menu-item index="/AccountGet" class="submenu-item">
                                <el-icon>
                                    <UserFilled />
                                </el-icon>
                                <span>用户管理</span>
                            </el-menu-item>
                        </el-sub-menu>

                        <el-sub-menu index="2" class="menu-submenu">
                            <template #title>
                                <el-icon>
                                    <Setting />
                                </el-icon>
                                <span>溯源管理</span>
                            </template>
                            <el-menu-item index="/Traceability" class="submenu-item">
                                <el-icon>
                                    <HelpFilled />
                                </el-icon>
                                <span>溯源管理</span>
                            </el-menu-item>
                        </el-sub-menu>

                        <el-sub-menu index="3" class="menu-submenu">
                            <template #title>
                                <el-icon>
                                    <Setting />
                                </el-icon>
                                <span>系统设置</span>
                            </template>
                            <el-menu-item index="/about" class="submenu-item">
                                <el-icon>
                                    <InfoFilled />
                                </el-icon>
                                <span>关于系统</span>
                            </el-menu-item>
                            <el-menu-item index="/home" class="submenu-item">
                                <el-icon>
                                    <Setting />
                                </el-icon>
                                <span>管理</span>
                            </el-menu-item>
                        </el-sub-menu>
                    </el-menu>
                </el-aside>
                <el-main>
                    <RouterView />
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>
<script lang="ts" setup>
import { axiosr } from '@/http/axiosRead'
import {
    Document,
    Menu as IconMenu,
    Location,
    Setting,
    House,
    User,
    Key,
    Avatar,
    InfoFilled,
    ArrowDown,
    Bell,
    SwitchButton
} from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

let BaseURL = import.meta.env.VITE_APP_API_URL

onMounted(() => {
    console.log(BaseURL);
})

const handleOpen = (key: string, keyPath: string[]) => {
    console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
    console.log(key, keyPath)
}

const router = useRouter()

const permission = localStorage.getItem("RoleId");

const permissions = ref()

const userName = ref(localStorage.getItem('name') || '用户')
const roleName = ref(localStorage.getItem('RoleName') || '角色')

const permis = () => {
    axiosr.post('/api/Permission/GetPermissionsByRoleIds', { roleIds: permission }).then((res) => {
        permissions.value = res.data.data
        console.log(res.data.data);
    })
}

function logout() {
    // 清除所有用户相关localStorage
    localStorage.removeItem('token')
    localStorage.removeItem('RoleId')
    localStorage.removeItem('RoleName')
    localStorage.removeItem('Uid')
    localStorage.removeItem('name')
    localStorage.removeItem('rememberName')
    localStorage.removeItem('rememberPwd')
    // 跳转到登录页
    router.replace('/')
}

function goUserProfile() {
    router.push('/user-profile')
}

function goSystemSetting() {
    router.push('/about')
}

onMounted(() => {
    permis()
})
</script>

<style scoped>
.common-layout {
    min-height: 100vh;
    background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
}

.el-header {
    background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
    color: #fff;
    padding: 0;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    position: relative;
    z-index: 1000;
    height: 70px !important;
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
    padding: 0 2rem;
}

.logo {
    display: flex;
    align-items: center;
    gap: 16px;
}

.logo-icon-wrapper {
    width: 48px;
    height: 48px;
    background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.logo-icon {
    font-size: 24px;
    color: #fff;
}

.logo-text-wrapper {
    display: flex;
    flex-direction: column;
}

.logo-text {
    font-size: 1.4rem;
    font-weight: bold;
    letter-spacing: 1px;
    color: #fff;
}

.logo-subtitle {
    font-size: 0.8rem;
    color: #94a3b8;
    letter-spacing: 0.5px;
}

.header-right {
    display: flex;
    align-items: center;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 16px;
}

.notification-btn {
    background: rgba(255, 255, 255, 0.1) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    color: #fff !important;
    transition: all 0.3s ease;
}

.notification-btn:hover {
    background: rgba(255, 255, 255, 0.2) !important;
    transform: translateY(-1px);
}

.user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    padding: 8px 16px;
    border-radius: 12px;
    transition: all 0.3s ease;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.user-info:hover {
    background: rgba(255, 255, 255, 0.1);
    transform: translateY(-1px);
}

.user-details {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}

.user-name {
    font-weight: 600;
    font-size: 0.9rem;
    color: #fff;
}

.user-role {
    font-size: 0.75rem;
    color: #94a3b8;
}

.el-aside {
    background: #fff;
    border-radius: 0 20px 20px 0;
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.08);
    margin: 1.5rem 0 1.5rem 1.5rem;
    overflow: hidden;
    position: relative;
}

.sidebar-header {
    padding: 1.5rem 2rem 1rem;
    border-bottom: 1px solid #e2e8f0;
    background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
}

.sidebar-title {
    font-size: 1rem;
    font-weight: 600;
    color: #475569;
    margin: 0;
}

.el-menu {
    border: none;
    height: calc(100% - 80px);
    padding: 1rem 0;
}

.menu-item,
.submenu-item {
    margin: 4px 1rem;
    border-radius: 12px;
    transition: all 0.3s ease;
    height: 48px;
    line-height: 48px;
}

.menu-item:hover,
.submenu-item:hover {
    background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%) !important;
    color: #fff !important;
    transform: translateX(8px);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.menu-item.is-active,
.submenu-item.is-active {
    background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%) !important;
    color: #fff !important;
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.menu-submenu .el-sub-menu__title {
    margin: 4px 1rem;
    border-radius: 12px;
    transition: all 0.3s ease;
    height: 48px;
    line-height: 48px;
}

.menu-submenu .el-sub-menu__title:hover {
    background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%) !important;
    color: #3b82f6 !important;
}

.el-main {
    padding: 2rem;
    background: #fff;
    border-radius: 20px 0 0 20px;
    box-shadow: -8px 0 32px rgba(0, 0, 0, 0.08);
    margin: 1.5rem 1.5rem 1.5rem 0;
    min-height: calc(100vh - 140px);
    overflow-y: auto;
}

@media (max-width: 768px) {
    .el-aside {
        width: 240px !important;
        margin: 1rem 0 1rem 1rem;
    }

    .el-main {
        margin: 1rem 1rem 1rem 0;
        padding: 1rem;
    }

    .header-content {
        padding: 0 1rem;
    }

    .logo-text {
        font-size: 1.2rem;
    }

    .user-details {
        display: none;
    }
}
</style>